package com.example.seataBusiness.controller;

import com.example.seataBusiness.common.response.ObjectResponse;
import com.example.seataBusiness.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/business")
@Slf4j
public class BusinessController {

    private final BusinessService businessService;

    public BusinessController(BusinessService businessService) {
        this.businessService = businessService;
    }

    /**
     * 模拟用户下单业务逻辑(正常情况)
     *
     * @param userId        用户ID
     * @param commodityCode 商品编号
     * @param count         购买数量
     * @param orderMoney    订单总金额
     */
    @PostMapping("/order")
    public ObjectResponse<Object> order(@RequestParam("userId") String userId,
                                @RequestParam("commodityCode") String commodityCode,
                                @RequestParam("count") Integer count,
                                @RequestParam("orderMoney") Double orderMoney) {
        return businessService.handleBusiness(userId, commodityCode, count, orderMoney);
    }

    /**
     * 模拟用户下单业务逻辑(异常全局回滚)
     *
     * @param userId        用户ID
     * @param commodityCode 商品编号
     * @param count         购买数量
     * @param orderMoney    订单总金额
     */
    @PostMapping("/orderError")
    public ObjectResponse<Object> orderError(@RequestParam("userId") String userId,
                                     @RequestParam("commodityCode") String commodityCode,
                                     @RequestParam("count") Integer count,
                                     @RequestParam("orderMoney") Double orderMoney) {
        return businessService.handleBusinessExceptiop(userId, commodityCode, count, orderMoney);
    }
}
